package com.example.micro.netty.server.thread;

import java.util.concurrent.locks.LockSupport;

/**
 * LockSupport 实现交替打印
 *
 * @author Code Farmer
 * @create 2020/10/19 7:51 下午
 */
public class AlternateLockSupportPrint {

    private static int index = 0;

    private static Thread threadOdd = null;
    private static Thread threadEven = null;


    public static void main(String[] args) {

        threadOdd = new Thread(() -> {
            while (true) {
                LockSupport.park();
                System.out.println(Thread.currentThread().getName() + ":" + index++);
                LockSupport.unpark(threadEven);
            }
        });


        threadEven = new Thread(() -> {
            while (true) {
                System.out.println(Thread.currentThread().getName() + ":" + index++);
                LockSupport.unpark(threadOdd);
                LockSupport.park();
            }
        });

        threadOdd.start();
        threadEven.start();

    }

}
